package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.common.LoginUser;
import com.xjh.him.dao.FriendMapper;
import com.xjh.him.dao.TrendMapper;
import com.xjh.him.domain.FriendDO;
import com.xjh.him.domain.TrendDO;
import com.xjh.him.domain.UserDO;
import com.xjh.him.dto.trend.TrendDTO;
import com.xjh.him.dto.trend.TrendPublishDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.PageDTO;
import org.xjh.tool.dto.ResultDTO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TrendServiceImpl implements ITrendService {

    @Autowired
    private TrendMapper trendMapper;

    @Autowired
    private FriendMapper friendMapper;

    @Override
    public ResultDTO<List<TrendDTO>> listTrend(PageDTO pageDTO) {
        UserDO loginUser = LoginUser.getLoginUser();
        List<FriendDO> friendDOList = friendMapper.queryFriend(loginUser.getId());
        List<Long> userIdList = friendDOList.stream().map(item -> {
            if (loginUser.getId().equals(item.getFirstId())) {
                return item.getSecondId();
            } else {
                return item.getFirstId();
            }
        }).collect(Collectors.toList());
        userIdList.add(loginUser.getId());

        QueryWrapper<TrendDO> listTrendQW = new QueryWrapper<>();
        listTrendQW.in("user_id", userIdList);
        listTrendQW.orderByDesc("id");
        listTrendQW.last(String.format("LIMIT %s, %s", pageDTO.getSkip(), pageDTO.getLimit()));
        List<TrendDO> trendDOList = trendMapper.selectList(listTrendQW);
        List<TrendDTO> trendDTOList = trendDOList.stream().map(item -> {
            TrendDTO dto = new TrendDTO();
            BeanUtils.copyProperties(item, dto);
            List<String> imgList = new ArrayList<>();
            if (!Checks.isEmpty(item.getImgContent())) {
                imgList.addAll(Arrays.asList(item.getImgContent().split(",")));
            }
            dto.setImgList(imgList);
            return dto;
        }).collect(Collectors.toList());
        return ResultDTO.getSuccessResult(trendDTOList);
    }

    @Override
    public ResultDTO<Boolean> publishTrend(TrendPublishDTO publishDTO) {
        if (Checks.isAnyEmpty(publishDTO, publishDTO.getTextContent())) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        UserDO loginUser = LoginUser.getLoginUser();
        TrendDO trendDO = new TrendDO();
        trendDO.setUserId(loginUser.getId());
        trendDO.setUserNickname(loginUser.getNickname());
        trendDO.setUserAvatar(loginUser.getAvatarUrl());
        trendDO.setTextContent(publishDTO.getTextContent());
        if (Checks.isEmpty(publishDTO.getImgUrlList())) {
            trendDO.setImgContent("");
        }else {
            trendDO.setImgContent(String.join(",", publishDTO.getImgUrlList()));
        }
        int insert = trendMapper.insert(trendDO);
        if (insert == 0) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_SYSTEM_BUSY);
        }
        return ResultDTO.getSuccessResult(true);
    }
}
